package com.yuba.front.controller.articles;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yuba.common.core.domain.R;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.mybatis.core.page.TableDataInfo;
import com.yuba.common.web.core.BaseController;
import com.yuba.front.domain.bo.ArticlesBo;
import com.yuba.front.domain.vo.ArticlesVo;
import com.yuba.front.service.IArticlesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识api接口
 */
@RestController
@RequestMapping("/api/articles")
@RequiredArgsConstructor
public class ApiArticlesController extends BaseController {
    private final IArticlesService articlesService;

    /**
     * 分页查询知识列表
     * @param pageQuery 分页条件
     * @param bo 查询条件
     * @return 知识列表
     */
    @SaIgnore
    @GetMapping("list")
    public R<TableDataInfo<ArticlesVo>> list(PageQuery pageQuery, ArticlesBo bo) {
        return R.ok(articlesService.queryPageList(bo, pageQuery));
    }

    /**
     * 返回热榜
     * @return 热榜数据；
     */
    @SaIgnore
    @GetMapping("hot")
    public R<List<ArticlesVo>> hot(@RequestParam(required = false) String categoryCode) {
        return R.ok(articlesService.queryHotList(categoryCode));
    }


    /**
     * 获取详情
     * @param id 知识id
     * @return 知识数据
     */
    @SaIgnore
    @GetMapping("detail")
    public R<ArticlesVo> detail(Long id) {
        return R.ok(articlesService.queryById(id));
    }

    @SaIgnore
    @PostMapping("view")
    public R<Void> views(@RequestBody ArticlesBo body) {
        if (body.getArticleId() == null) {
            return R.ok();
        }
        articlesService.views(body.getArticleId());
        return R.ok();
    }
}
